package com.atlin.project.controller;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.dto.stockIssuanceInfo.StockIssuanceInfoQueryRequest;
import com.atlin.project.model.fm.domain.StockIssuanceInfo;
import com.atlin.project.model.vo.StockIssuanceInfoVO;
import com.atlin.project.service.SysUserService;
import com.atlin.project.service.fm.StockIssuanceInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 可转债接口类
 *
 * @author atlin
 */
@RestController
@RequestMapping("/stockIssuanceInfo")
public class StockIssuanceInfoController {

    @Resource
    private StockIssuanceInfoService stockIssuanceInfoService;

    @Resource
    private SysUserService sysUserService;


    // region 增删改查


    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteStockIssuanceInfo(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser user = sysUserService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        StockIssuanceInfo oldstockIssuanceInfo = stockIssuanceInfoService.getById(id);
        ThrowUtils.throwIf(oldstockIssuanceInfo == null, ErrorCode.NULL_ERROR);

        boolean b = stockIssuanceInfoService.removeById(id);
        return ResultUtils.success(b);
    }


    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<StockIssuanceInfo> getStockIssuanceInfoById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StockIssuanceInfo stockIssuanceInfo = stockIssuanceInfoService.getById(id);
        if (stockIssuanceInfo == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        return ResultUtils.success(stockIssuanceInfo);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<StockIssuanceInfoVO> getStockIssuanceInfoVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        StockIssuanceInfo stockIssuanceInfo = stockIssuanceInfoService.getById(id);
        if (stockIssuanceInfo == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        StockIssuanceInfoVO stockIssuanceInfoVO = StockIssuanceInfoVO.objToVo(stockIssuanceInfo);
        return ResultUtils.success(stockIssuanceInfoVO);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param stockIssuanceInfoQueryRequest 查询参数
     * @param request                       查询请求
     * @return 分页封装类
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<StockIssuanceInfoVO>> listStockIssuanceInfoVOByPage(@RequestBody StockIssuanceInfoQueryRequest stockIssuanceInfoQueryRequest,
                                                                                 HttpServletRequest request) {
        long current = stockIssuanceInfoQueryRequest.getCurrent();
        long size = stockIssuanceInfoQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<StockIssuanceInfo> stockIssuanceInfoPage = stockIssuanceInfoService.page(new Page<>(current, size),
                stockIssuanceInfoService.getQueryWrapper(stockIssuanceInfoQueryRequest));

        Page<StockIssuanceInfoVO> stockIssuanceInfoVOPage = stockIssuanceInfoService.getStockIssuanceInfoVOPage(stockIssuanceInfoPage, request);

        return ResultUtils.success(stockIssuanceInfoVOPage);
    }

    /**
     * 分页获取列表（仅管理员）
     *
     * @param stockIssuanceInfoQueryRequest 查询参数
     * @param request                       查询请求
     * @return 分页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<StockIssuanceInfo>> listStockIssuanceInfoByPage(@RequestBody StockIssuanceInfoQueryRequest stockIssuanceInfoQueryRequest,
                                                                             HttpServletRequest request) {
        long current = stockIssuanceInfoQueryRequest.getCurrent();
        long size = stockIssuanceInfoQueryRequest.getPageSize();
        Page<StockIssuanceInfo> stockIssuanceInfoPage = stockIssuanceInfoService.page(new Page<>(current, size),
                stockIssuanceInfoService.getQueryWrapper(stockIssuanceInfoQueryRequest));
        return ResultUtils.success(stockIssuanceInfoPage);
    }

    // endregion

}
